<?php

$localIp = file_get_contents('http://imail.sinaapp.com/ip.php');
if (!$localIp) {
    $localIp = file_get_contents('http://imail.sinaapp.com/ip.php');
}

//每天执行2次
require_once dirname(__FILE__) . '/autoload.php';
require_once dirname(__FILE__) . '/curl_proxy.php';

$timeStart = time();

$GlobalResultData = array();

$ShouRuData = array();
if (file_exists(dirname(__FILE__) . '/report_7day.csv')) {
    $fp = fopen(dirname(__FILE__) . '/report_7day.csv', 'r');
    $INDEX = 0;
    while ($dataline = fgets($fp)) {
        if (!$INDEX) {
            $INDEX++;
            continue;
        }
        $dataline = explode("\t", $dataline);
        $dataline[0] = preg_replace('#\s+#', '', $dataline[0]);
        $arrDoamin = explode('.', $dataline[0]);
        $dataline[0] = $arrDoamin[1] . '.' . $arrDoamin[2];
        $ShouRuData[$dataline[0]] = preg_replace('#\s+#', '', $dataline[6]);
        $INDEX++;
    }
    fclose($fp);
}

$ShouRuDataOld = array();
if (file_exists(dirname(__FILE__) . '/report_7day_old.csv')) {
    $fp = fopen(dirname(__FILE__) . '/report_7day_old.csv', 'r');
    $INDEX = 0;
    while ($dataline = fgets($fp)) {
        if (!$INDEX) {
            $INDEX++;
            continue;
        }
        $dataline = explode("\t", $dataline);
        $dataline[0] = preg_replace('#\s+#', '', $dataline[0]);
        $arrDoamin = explode('.', $dataline[0]);
        $dataline[0] = $arrDoamin[1] . '.' . $arrDoamin[2];
        $ShouRuDataOld[$dataline[0]] = preg_replace('#\s+#', '', $dataline[6]);
        $INDEX++;
    }
    fclose($fp);
}


$ShouRuData30day = array();
if (file_exists(dirname(__FILE__) . '/report_30day.csv')) {
    $fp = fopen(dirname(__FILE__) . '/report_30day.csv', 'r');
    $INDEX = 0;
    while ($dataline = fgets($fp)) {
        if (!$INDEX) {
            $INDEX++;
            continue;
        }
        $dataline = explode("\t", $dataline);
        $dataline[0] = preg_replace('#\s+#', '', $dataline[0]);
        $arrDoamin = explode('.', $dataline[0]);
        $dataline[0] = $arrDoamin[1] . '.' . $arrDoamin[2];
        $ShouRuData30day[$dataline[0]] = preg_replace('#\s+#', '', $dataline[6]);
        $INDEX++;
    }
    fclose($fp);
}

$ShouRuData30dayOld = array();
if (file_exists(dirname(__FILE__) . '/report_30day_old.csv')) {
    $fp = fopen(dirname(__FILE__) . '/report_30day_old.csv', 'r');
    $INDEX = 0;
    while ($dataline = fgets($fp)) {
        if (!$INDEX) {
            $INDEX++;
            continue;
        }
        $dataline = explode("\t", $dataline);
        $dataline[0] = preg_replace('#\s+#', '', $dataline[0]);
        $arrDoamin = explode('.', $dataline[0]);
        $dataline[0] = $arrDoamin[1] . '.' . $arrDoamin[2];
        $ShouRuData30dayOld[$dataline[0]] = preg_replace('#\s+#', '', $dataline[6]);
        $INDEX++;
    }
    fclose($fp);
}


$s = '';
if (file_exists(dirname(__FILE__) . '/domainlist.txt')) {
    $s = file_get_contents(dirname(__FILE__) . '/domainlist.txt');
}

$domainlist = array_filter(explode('<br />', nl2br(trim($s))));
foreach ($domainlist as &$ds) {
    $ds = preg_replace('#\s+#', '', $ds);
}unset($ds);
$domainlist = array_filter($domainlist);

$locallockfile = dirname(__FILE__) . '/locklist.php';
if (!file_exists($locallockfile)) {
    log::e('!file_exists($locallockfile)', true, true);
    die();
}
$lockdomains = json_decode(file_get_contents($locallockfile), true);
$arrUniqDomainDataUniq = array();

$locallockfile = dirname(__FILE__) . '/locklist_1.php';
if (!file_exists($locallockfile)) {
    log::w('!file_exists($locallockfile)', true, true);
}
$lockdomains_1 = json_decode(file_get_contents($locallockfile), true);
$lockdomains = array_merge($lockdomains, $lockdomains_1);

$arrUniqDomainDataWWW = array_merge($domainlist, $lockdomains);
foreach ($arrUniqDomainDataWWW as $domainALL) {
    $arrDInfo = explode('.', $domainALL);
    $length = count($arrDInfo);
    $extk = $length - 1;
    $dok = $length - 2;
    $domain = $arrDInfo[$dok] . '.' . $arrDInfo[$extk];
    if (is_numeric($arrDInfo[$extk])) {
        log::w($domain);
        continue;
    }
    $arrUniqDomainDataUniq[] = $domain;
}

$arrUniqDomainDataUniq = array_filter($arrUniqDomainDataUniq);
$arrUniqDomainDataUniq = array_unique($arrUniqDomainDataUniq);

$TotalCount = count($arrUniqDomainDataUniq);
$WarningCount = 0;

$HTMLBODY = '';
$HTMLBODY.= '<b>[' . $localIp . ']hichina域名过期时间统计(' . $TotalCount . '个)：</b></div>
          <table border="1" width="100%" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" style="border-collapse: collapse;font-size: 14px;">';
$HTMLBODY.='<tr height="35">
    <td width="100">域名&nbsp;</td>
    <td width="50">7天收入&nbsp;</td>
    <td width="50">30天收入&nbsp;</td>
    <td width="50">对比收入&nbsp;</td>
    <td width="150">注册/过期时间&nbsp;</td>
    <td width="80">IP&nbsp;</td>
    <td width="100">DNS&nbsp;</td>
    <td width="100">距离过期时间</td></tr>
    ';
$HOSTIPDATA = array();
$IPSHOURUDATA = array();
foreach ($arrUniqDomainDataUniq as $domain) {
    if (file_exists(dirname(__FILE__) . '/Runtime/data/IP_' . $domain . '.txt')) {
        $HOSTIPDATA[$domain] = file_get_contents(dirname(__FILE__) . '/Runtime/data/IP_' . $domain . '.txt');
    } else {
        $HOSTIPDATA[$domain] = gethostbyname('www.' . $domain);
        if ($HOSTIPDATA[$domain]) {
            file_put_contents(dirname(__FILE__) . '/Runtime/data/IP_' . $domain . '.txt', $HOSTIPDATA[$domain]);
        }
    }

    log::n($domain . ':' . $HOSTIPDATA[$domain]);
}

foreach ($arrUniqDomainDataUniq as $domain) {
    $arrDateTime = domain_exp_check($domain);
    //距离过期时间
    $time = strtotime($arrDateTime['expdate']) - time();
    $days = floor($time / 86400);
    if ($days <= 90) {
        $color = ' color="red" ';
        $WarningCount++;
    } else {
        $color = '';
    }
    if ($days >= 10000) {
        $arrDateTime = domain_exp_check($domain);
        $time = strtotime($arrDateTime['expdate']) - time();
        $days = floor($time / 86400);
        if ($days <= 90) {
            $color = ' color="red" ';
            $WarningCount++;
        } else {
            $color = '';
        }
        if ($days >= 10000) {
            $arrDateTime = domain_exp_check($domain);
            $time = strtotime($arrDateTime['expdate']) - time();
            $days = floor($time / 86400);
            if ($days <= 90) {
                $color = ' color="red" ';
                $WarningCount++;
            } else {
                $color = '';
            }
        }
    }

    if ($color == ' color="red" ') {

        $GlobalResultData['red'][] = '<tr height="35">
    <td><font' . $color . '>' . $domain . '</font></td>
    <td>' . $ShouRuData[$domain] . '</td>
    <td>' . $ShouRuData30day[$domain] . '</td>
    <td>' . $ShouRuDataOld[$domain] . '/' . $ShouRuData30dayOld[$domain] . '</td>
    <td><font' . $color . '>' . $arrDateTime['regdate'] . '/' . $arrDateTime['expdate'] . '</font></td>
    <td>' . $HOSTIPDATA[$domain] . '</td>
    <td>' . $arrDateTime['dnsserver'] . '</td>
    <td><font' . $color . '>' . $days . '</font>&nbsp;天</td></tr>
    ';
    } else {
        $GlobalResultData['green'][] = '<tr height="35">
    <td><font' . $color . '>' . $domain . '</font></td>
    <td>' . $ShouRuData[$domain] . '</td>
    <td>' . $ShouRuData30day[$domain] . '</td>
    <td>' . $ShouRuDataOld[$domain] . '/' . $ShouRuData30dayOld[$domain] . '</td>
    <td><font' . $color . '>' . $arrDateTime['regdate'] . '/' . $arrDateTime['expdate'] . '</font></td>
    <td>' . $HOSTIPDATA[$domain] . '</td>
    <td>' . $arrDateTime['dnsserver'] . '</td>
    <td><font' . $color . '>' . $days . '</font>&nbsp;天</td></tr>
    ';
    }
    $IPSHOURUDATA[$HOSTIPDATA[$domain]]['30day']+=$ShouRuData30day[$domain];
    $IPSHOURUDATA[$HOSTIPDATA[$domain]]['7day']+=$ShouRuData[$domain];
    $IPSHOURUDATA[$HOSTIPDATA[$domain]]['7dayold']+=$ShouRuDataOld[$domain];
    $IPSHOURUDATA[$HOSTIPDATA[$domain]]['30dayold']+=$ShouRuData30dayOld[$domain];
}

foreach ($GlobalResultData['red'] as $html) {
    $HTMLBODY.=$html;
}

foreach ($GlobalResultData['green'] as $html) {
    $HTMLBODY.=$html;
}

$HTMLBODY.='</table>';

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$arrUsers = array(
    'think889977@sina.com',
);

$timeEnd = time();
$HTMLBODY.='<hr />耗时:' . ($timeEnd - $timeStart) . '秒<hr />';
$HTMLBODY.=implode('<br />', $arrUniqDomainDataUniq) . '<hr /><pre>';
$HTMLBODY.=print_r($IPSHOURUDATA, true) . '</pre><hr>';


$HTMLBODY_IPSHOURU = '';
$HTMLBODY_IPSHOURU.= '<b>[' . $localIp . ']IP机器收入统计：</b></div>
          <table border="1" width="100%" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" style="border-collapse: collapse;font-size: 14px;">';
$HTMLBODY_IPSHOURU.='<tr height="35">
    <td width="100">IP地址</td>
    <td width="100">7天收入&nbsp;</td>
    <td width="100">30天收入&nbsp;</td>
    <td width="100">7天对比&nbsp;</td>
    <td width="100">30天对比&nbsp;</td>
    ';
foreach ($IPSHOURUDATA as $IPADDR => $arrData) {
    $HTMLBODY_IPSHOURU.='<tr height="35">
    <td width="100">' . $IPADDR . '</td>
    <td width="100">' . $arrData['7day'] . '</td>
    <td width="100">' . $arrData['30day'] . '</td>
    <td width="100">' . $arrData['7dayold'] . '</td>
    <td width="100">' . $arrData['30dayold'] . '</td>
    ';
}
$HTMLBODY_IPSHOURU.='</table>';
$HTMLBODY.=$HTMLBODY_IPSHOURU . '<hr />';

post_email('[警告:' . $WarningCount . ']每日域名自动检测报告[' . date('Y-m-d H:i:s') . ']', $HTMLBODY, $arrUsers);

/**
 * 域名过期查询
 * @param string $domain
 * @return boolean
 */
function domain_exp_check($domain) {
    fCheckDir(dirname(__FILE__) . '/Runtime/data/EXPDATE_HICHINA/EXP_' . $domain . '.txt');
    if (file_exists(dirname(__FILE__) . '/Runtime/data/EXPDATE_HICHINA/EXP_' . $domain . '.txt') && time() - filemtime(dirname(__FILE__) . '/Runtime/data/EXPDATE_HICHINA/EXP_' . $domain . '.txt') < 86400 * 30) {
        $json = file_get_contents(dirname(__FILE__) . '/Runtime/data/EXPDATE_HICHINA/EXP_' . $domain . '.txt');
        return json_decode($json, true);
    }
    //sleep(5);
    $arr = explode('.', $domain);
    $length = count($arr);
    $extk = $length - 1;
    $dok = $length - 2;
    $domain = $arr[$dok] . '.' . $arr[$extk];
    if (!in_array($arr[$extk], array('com', 'cn', 'net', 'org', 'info', 'cc'))) {
        return array('regdate' => '2013-01-01', 'expdate' => '2069-01-01', 'dnsserver' => 'Other Ext');
        ;
    }

    $whoisurl = 'http://whois.hichina.com/whois/api_whois?host=' . $domain . '&oper=0';

    $s = curl_string($whoisurl);
    if (!$s) {
        $s = curl_string($whoisurl);
    }
    $s = json_decode($s, true);
    $dns = explode(',', $s['name_server']);
    $s['exp_date'] = preg_replace('#\s+#', '', $s['exp_date']);
    $result = array('regdate' => $s['reg_date'], 'expdate' => $s['exp_date'], 'dnsserver' => $dns[0]);
    if ($s['reg_date'] && $s['exp_date'] && $dns[0]) {
        file_put_contents(dirname(__FILE__) . '/Runtime/data/EXPDATE_HICHINA/EXP_' . $domain . '.txt', json_encode($result));
    }
    return $result;
}

?>